//===============================================================================
// Microsoft User Interface Process Application Block for .NET Quick Start
// http://msdn.microsoft.com/library/en-us/dnbda/html/uip.asp
//
// FormValidator.cs
//
// This file contains the implementations of the FormValidator class.
//
// 
//===============================================================================
// Copyright (C) 2000-2001 Microsoft Corporation
// All rights reserved.
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR
// FITNESS FOR A PARTICULAR PURPOSE.
//==============================================================================

using System.Windows.Forms;

namespace ClientForms
{
    /// <remarks>
    /// 
    /// </remarks>
    public sealed class FormValidator
    {
        #region Delegates

        /// <summary>
        /// Delegate that defines a method that can perform validation against a control
        /// </summary>
        public delegate bool ControlValidator(Control control);

        #endregion

        private FormValidator()
        {
        }


        /// <summary>
        /// Method that determines whether the contents of a field are valid
        /// </summary>
        /// <param name="errNotifier">ErrorProvider component that will be used to provide Error notification</param>
        /// <param name="control">Control that is being validated ( also the one that the ErrorProvider component will
        /// render itself next to if an Error occurs.</param>
        /// <param name="errMessage">String containing the Error message to show if the validation fails</param>
        /// <param name="validator">Delegate that points to a method that will perform the validation</param>
        /// <returns></returns>
        public static bool Fieldisvalid(ErrorProvider errnotifier, Control control, string errmessage, ControlValidator validator)
        {
            bool isvalid = false;

            // invoke the method that will perform the validation against the control
            if (!validator(control))
            {
                // if the validation did not pass then set the Error message next to the control
                errnotifier.SetError(control, errmessage);
                isvalid = false;
            }
            else
            {
                // clear the Error message associated with the control
                errnotifier.SetError(control, "");
                isvalid = true;
            }

            return isvalid;
        }


        /// <summary>
        /// Method that matches the ControlValidator signature and will perform a check to see if the text property of a control
        /// has been set to anything other than a zero length string
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public static bool Hastextvalidator(Control control)
        {
            return control.Text.Trim().Length > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        public static bool Isnumericvalidator()
        {
            return true;
        }

        /// <summary>
        ///		  validate the email form entry
        /// </summary>
        /// <param name="control"></param>
        /// <returns></returns>
        public static bool Isemailvalidator(Control control)
        {
            if (control.Text.Length <= 0)
            {
                return false;
            }
            else
            {
                return control.Text.IndexOf("@", 0, control.Text.Length) >= 0;
            }
        }
    }

    /// <remarks>
    /// Class that handles control validation results
    /// </remarks>
    public class Validationresult
    {
        private string _errormessage;
        private bool _isvalid;

        /// <summary>
        /// The constructor for the ValidationResult slass
        /// </summary>
        /// <param name="isValid">Boolean for whether the control has valid input or not.</param>
        /// <param name="errorMessage">The Error message to append to the control err notifier.</param>
        public Validationresult(bool isvalid, string errormessage)
        {
            _isvalid = isvalid;
            _errormessage = errormessage;
        }

        /// <summary>
        /// Get the control Error message.
        /// </summary>
        /// <value>The Error message for the control.</value>
        public string Errormessage
        {
            get { return _errormessage; }
        }

        /// <summary>
        /// Get the control valid status.
        /// </summary>
        /// <value>The status of the control.</value>
        public bool Isvalid
        {
            get { return _isvalid; }
        }
    }
}